Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRM-18082 - Allow case API create to work with custom data #10728

Merged
merged 11 commits into from
Aug 14, 2017

Conversation

mattwire
Copy link
Contributor

@mattwire mattwire commented Jul 23, 2017

This PR adds support for custom data for Case.Create API in line with other *.Create API functions. It refactors Case.Create API to support updating as the Case.Update API function is deprecated. This means a single codepath is now used for creation and updating, inline with other parts of CiviCRM API.

@mattwire
Copy link
Contributor Author

@colemanw @aydun Would you mind a quick review of this when you get a moment, as you are linked on the original jira issue

Copy link
Member

@davialexandre davialexandre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mattwire Left some inline comments. Besides that, it would be great if the PR description had details about the changes you did and their reasons. I tried to compare them to the suggestions made on the ticket's comments, but it seems you did a bit more than what is suggested there. Those details also helper reviewers to better understand what was the intent.

Finally, do you think you can add tests to this?

api/v3/Case.php Outdated
throw new API_Exception('Invalid case_type_id. No such case type exists.');
if (empty($params['id'])) {
// Creating a new case, so make sure we have the necessary parameters
civicrm_api3_verify_mandatory($params, NULL, [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since PHP 5.3 is still supported, we can't use short array syntax yet

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

api/v3/Case.php Outdated
throw new API_Exception(ts('You cannot update creator id'));
}

$mCaseId = $origContactIds = array();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does the m mean here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not my code :-) But agreed, it means merged so I've renamed to $mergedCaseId

api/v3/Case.php Outdated
'end_date' => CRM_Utils_Array::value('end_date', $params),
'subject' => $params['subject'],
);
// return error if modifying creator id
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the code is quite self-explanatory, so there's no need to this comment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed comment

api/v3/Case.php Outdated
@@ -60,37 +60,67 @@
* api result array
*/
function civicrm_api3_case_create($params) {
// Process params
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of adding this comment here, I think it would be better to extract the logic to a function with a descriptive name

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to _civicrm_api_case_format_params()

api/v3/Case.php Outdated
// get original contact id and creator id of case
if (!empty($params['contact_id'])) {
$origContactIds = CRM_Case_BAO_Case::retrieveContactIdsByCaseId($params['id']);
$origContactId = $origContactIds[1];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 1 and not 0 or 2?

Also, what is the difference between origContactId and origContactIds?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a comment, again not my code, but copied from the update function. That function returns a 1-based array, as it's used elsewhere it's not in scope for me to refactor right now! But I've added a FIXME comment there explaining. origContactId is an integer, origContactIds an array of contactIds, again copied from update function.

api/v3/Case.php Outdated
@@ -101,31 +131,46 @@ function civicrm_api3_case_create($params) {
CRM_Case_BAO_CaseContact::create($contactParams);
}

if (!isset($params['id'])) {
// Creating a new case
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this comment correct? Are we really creating a case here? If not, please use the comment to explain why do we need this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are, I don't really understand the xml stuff very well, except that cases are defined in xml files, and when creating a new one you have to read the xml file to get case params/setup the case. I've added a comment to that effect - the code in _civicrm_api3_case_create_xmlProcessor is simply extracted code from the original api_v3_case_create function.

api/v3/Case.php Outdated
return civicrm_api3_create_success($values, $params, 'Case', 'create', $caseBAO);
}

function _civicrm_api3_case_create_xmlProcessor($params, $caseBAO) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about a docblock for this function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

api/v3/Case.php Outdated
'caseID' => CRM_Utils_Array::value('id',$params),
'subject' => CRM_Utils_Array::value('subject',$params),
'location' => CRM_Utils_Array::value('location',$params),
'activity_date_time' => CRM_Utils_Array::value('start_date',$params),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a space between the params of every CRM_Utils_Array::value() call here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

api/v3/Case.php Outdated
throw new API_Exception('Case is linked with more than one contact id. Provide the required params orig_contact_id to be replaced');
}
$origContactId = $params['orig_contact_id'];
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this logic to get the original contact ID should be extracted to a separate function of even to a BAO method. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I've added a FIXME comment to that effect

api/v3/Case.php Outdated
$params['case_type'] = $caseTypes[$params['case_type_id']];
}
}
elseif (empty($params['case_type'])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Early returns would improve this method's readability and considerably reduce the indentation level in this method

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rearranged a little

@mattwire mattwire changed the title CRM-18082 - Allow case API create to work with custom data WIP CRM-18082 - Allow case API create to work with custom data Aug 4, 2017
@mattwire
Copy link
Contributor Author

mattwire commented Aug 4, 2017

Pending unit test and response to @davialexandre (many of your comments are relating to existing code that I didn't modify, but you make some good observations, I'll review and update the PR appropriately).

@mattwire
Copy link
Contributor Author

mattwire commented Aug 7, 2017

Unit tests now added

@mattwire
Copy link
Contributor Author

mattwire commented Aug 7, 2017

@davejenx Could you do a quick review as it's in use on one of your sites :-)

api/v3/Case.php Outdated
);
_civicrm_api3_case_format_params($params);
if (array_key_exists('creator_id', $params)) {
throw new API_Exception(ts('You cannot update creator id'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tiny picky point: I believe ts() isn't generally used for API exceptions. Discussed at https://chat.civicrm.org/civicrm/pl/qiotz5x6p7fxdegbiece7xhhmc . ts() isn't used for the other exceptions in this file, apart from an identical instance in civicrm_api3_case_update() which presumably this was copied from.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that's now fixed

@davejenx
Copy link
Contributor

davejenx commented Aug 7, 2017

@mattwire Haven't spotted any other issues - as I understand it, the change largely consists of incorporating the relevant parts of civicrm_api3_case_update() into civicrm_api3_case_create() so that it can serve both purposes, so much of it is existing code. If the existing & new unit tests are all passing, there's nothing else that jumps out at me.
@aydun made some good comments on the JIRA issue so as you said it would be good if he cast an eye over it.

@mattwire mattwire changed the title WIP CRM-18082 - Allow case API create to work with custom data CRM-18082 - Allow case API create to work with custom data Aug 7, 2017
api/v3/Case.php Outdated
// Add custom data
if (isset($params['custom'])) {
_civicrm_api3_custom_data_get($values[$caseBAO->id], TRUE, 'case', $caseBAO->id);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an extra database hit, which we normally don't do for create operations. There is an api option called something like "force_reload" which takes care of this type of thing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@colemanw Ok, I've had a good look through the code for various other APIs and I can't find any reference to this. Agreed that it's not ideal calling get again, but not sure what else I can do here to return back the customdata if it was passed in on create.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is that necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it's not :-) Removed it now, all tests still passing

api/v3/Case.php Outdated
// FIXME: CRM_Case_BAO_Case::retrieveContactIdsByCaseId returns a 1-based array (1 is the first element)
// It should really return a 0-based array for consistency.
$origContactIds = CRM_Case_BAO_Case::retrieveContactIdsByCaseId($params['id']);
$origContactId = $origContactIds[1];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use CRM_Utils_Array::first() so you don't have to worry about how the array is indexed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good

@mattwire
Copy link
Contributor Author

@colemanw Think this is now good to merge?

@colemanw colemanw merged commit 1a0ecaf into civicrm:master Aug 14, 2017
@mattwire mattwire deleted the CRM-18082_civicase-api-custom-data branch August 28, 2017 12:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants